//基础列表页面，不含状态筛选与添加按钮
var basePage = require('../../../../common/BasePage');
var baseList = require('../../../../common/BaseList');
Page({
  behaviors: [basePage, baseList],

  /**
   * 页面的初始数据
   */
  data: {
    /**是否播放视频/视频地址 */
    showVideo:false,
    reqUrl: '/home/ump/sweep/product',
    /**预支付地址 */
    preUrl: '/home/ump/sweep/preinfo',
    /**购买弹窗 */
    addCart: false,
    /**购买数量 */
    nums: 1,
    /**选中的产品 */
    pdc: {},
    /**分享链接 */
    path:'/pages/cls/list/BaseList',
    /**页面标题 */
    title:'',
	catIndex:0,
	catArr: [
		{
			loading: false,
			title: '采购推荐',
			current_page: 0,
			pages: 1,
			data: [],
			url:"/home/supplier/product/index",
			reqData: {
				purchase : 1
			}
		},
		{
			loading: false,
			title: '预售活动',
			current_page: 0,
			pages: 1,
			url:"/home/yuyue/index",
			data: [],
			
		},
		{
			loading: false,
			title: '天天特价',
			current_page: 0,
			pages: 1,
			url:"/home/supplier/product/index",
			data: [],
			reqData: {
				type:2
			}
			
		},
		{
			loading: false,
			title: '分类商品',
			current_page: 0,
			pages: 1,
			url:"/home/supplier/product/index",
			data: [],
			reqData: {
				cate_id:""
			}
			
    },
    {
			loading: false,
			title: '实物购买',
			current_page: 0,
			pages: 1,
			url:"/home/supplier/product/index",
			data: [],
			reqData: {
				is_video:1
			}
			
		}
	],
	itemArr:[],
	current_page:0,
    pages:1,
     showstate:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		console.log("wh_id",getApp().globalData.warehouse.wh_id)
	  if(options.title){
	    wx.setNavigationBarTitle({
	      title:  options.title,
	    })
	  }
	  
	  if(options.type==3){
		  let catArr=this.data.catArr
		  catArr[3].reqData.cate_id=options.cate_id
		  this.setData({
			  catArr:catArr
		  })
	  }else if(options.type==2){
          this.setData({
             showstate:true
          })
      }
	this.setData({
		catIndex:options.type,
		wh_id:options.wh_id
	})
	this.req_cat_data()
  },
  /**触底加载下一页 */
  onReachBottom() {

  	this.req_cat_data()
  },
  onPullDownRefresh() {
    console.log("下拉刷新");
    this.data.current_page = 0;
    this.req_cat_data()
  },
  nums_change(e) {
		// this.data.nums = e.detail;
		this.setData({
			nums: e.detail
		})
		console.log("nums_change", e)
	},
  req_cat_data() {
	  let catIndex=this.data.catIndex;
  	let cat = this.data.catArr[catIndex];
  	if (this.data.current_page == this.data.pages) {
  		return console.log("没有更多了");
  	}
  	cat.loading = true;
  	this.setData({
  		catArr: this.data.catArr
  	})
  	this.request({
  		url: this.data.host + cat.url ,
  		data: {
  			...cat.reqData,
  			//  limit:1,
  			page: (this.data.current_page * 1) + 1,
  			wh_id:this.data.wh_id,
  			limit:10,
  		}
  	}, res => {
  		this.get_pdcs_ok(catIndex, res);
  	});
  },
  get_pdcs_ok(catIndex, res) {
  	let list=this.data.itemArr
  	res.data.map(val => {
  		val.thumb = this.get_thumb(val.product_img);
  		// console.log("val.thumb",val.thumb);
  	});
  	res.current_page == 1 ? list = res.data : list = list.concat(res.data);
  	this.setData({
  		itemArr:list,
		current_page:res.current_page,
		pages:res.pages
  	});
  	wx.stopPullDownRefresh({
  		success: (res) => {},
  	})
  	// this.setData({
  	//   pdcArr: res.data
  	// })
  },
  
  /**
   * 收藏点击
   */
  collect_click(e) {
  	// console.log("collect_click", e);
  	let pdc = e.detail;
  	let index = e.currentTarget.dataset.index;
  	let status = pdc.collected ? 0 : 1;
  	this.request({
  		url: this.data.host + '/home/user/collect/collect',
  		data: {
  			status: status,
  			can_id: pdc.pid,
  			type: 1
  		},
  		method: 'post'
  	}, res => {
  		console.log("collect_ok", res);
  
  		let item = this.data.itemArr
  		item[index].collected = status;
  		item[index].collects = status ? (item[index].collects * 1) + 1 : item[index].collects - 1;
  		this.setData({
  			itemArr: item
  		})
  	});
  },
  /**
   * 添加购物车按钮点击事件
   * @param {*} param0 
   */
  add_cart_click(e) {
  	let pdc = e.detail;
  	console.log("秒杀",pdc)
  	this.setData({
  		nums: 1,
  		addCart: true,
  		pdc:pdc,
  		sku: pdc.sku,
  		product_id: pdc.pid,
  		product_name: pdc.product_name,
  		carTitle:e.currentTarget.dataset.titlt
  
  	});
  },
  /**
   * 添加产品到购物车
   */
  add_cart() {
  	console.log("add_cart", this.data.nums, this.data.product_id);
  	this.request({
  		url: this.data.host + '/home/cart/addToCart',
  		data: {
  			pid: this.data.product_id,
  			nums: this.data.nums,
  		},
  		method: 'post'
  	}, this.add_cart_ok);
  	
	},
	onCloseCart(){
		this.setData({
			addCart:false
		})
	},
	//立即购买
	buy_good() {
		console.log("立即购买", this.data.pdc)
		let data = {
			pid: this.data.product_id,
			nums: this.data.nums,
			wh_id: getApp().globalData.warehouse.wh_id,
			from :"flower"
		}

		this.request({
			url: this.data.host + '/home/flower/preInfo',
			data: data,
			method: 'get'
		}, res => {
			this.setData({
				addCart:false
			})
			res.data.cashier_decca = this.get_thumb(res.cashier_decca, 'wide');
			let that = this;
			wx.navigateTo({
				// url: '/pages/order_confirm/order_confirm',
				url: '/pages/neworder_confirm/order_confirm',
				success: function (obj) {
					// 通过eventChannel向被打开页面传送数据

					obj.eventChannel.emit('orderData', {
						wh_id: getApp().globalData.warehouse.wh_id,
						// index: that.data.areaData[that.data.tabIndex].index,
						data: res.data,
						m_addr: res.m_addr,
						activeCar: "flower",
						wuliu: res.data.freight_list,
						specialList: res.data.extra_list,
						pack_list: res.data.pack_list,
						buyType:1,
						order_pei_type:res.order_pei_type
					})
				}
			})
		});
	},
  /**
   * 添加购物车结果处理
   * @param {*} res  后台返回的数据
   */
  add_cart_ok(res) {
  	// console.log("add_cart_ok",res);
		// this.setData({nums:1})
		this.setData({
			addCart:false
		})
  	wx.showToast({
  		title: res.msg,
  		icon: 'success'
  	})
  },
  /**
   * 播放视频
   */
  video_click(e){
    this.setData({
      showVideo: this.data.imgHost + '/' + e.detail.video
    })
  },
  
  onShareAppMessage(res) {
  	console.log("onShareAppMessage", res);
  	let path = "/pages/cls/index/index?uid="+getApp().globalData.userInfo.uid;
  	let title = "";
  	if(res&&res.target){
  		let pid = res.target.dataset.pid;
  		let product_id = res.target.dataset.product_id;
  		if (pid) {
  			path = '/pages/cls/pdcList/Index?pid=' + pid + '&product_id=' + product_id+'&uid='+getApp().globalData.userInfo.uid;
  			title = "您的朋友向你推荐了一款鲜花"
  		}
		}
  	return {
  		title: title,
  		path: path,
  		// promise 
  	}
  },
})